#include <string>
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
//#include "opencv.hpp"
#include "MorphologyFeatExtractor.h"
#include "RecognizerModule.h"

//#define USE_DSHOW
#ifdef USE_DSHOW
	#include "videoInput.h" // DShow capture library
#endif
#include "svm.h"

/*******************************************************************/
using namespace std;
//using namespace cv;
/*******************************************************************/
void main(void)
{
#ifdef USE_DSHOW
	int camNum = 0;
	videoInput VI; 
	int numDevices = VI.listDevices();
	VI.setupDevice(camNum); 
	unsigned char* buffer = new unsigned char[VI.getSize(camNum)];
#else
	CvCapture * capture = NULL;
	capture = cvCreateCameraCapture(-1);
	if( NULL == capture )
	{	
		cout << "Could not capture the camera" << endl; 
		cvReleaseCapture(&capture);	
	}

#endif
	string svmFile = "NailScrewClassifier";
	string svmPath = ".\\..\\..\\Data\\";
	string savePath = ".\\..\\..\\Data\\nuts\\";
	string fname = "datapoint";
	string ext = ".jpg";
	int index=0;
	CMorphologyFeatExtractor morphFeatureExtractor;
	CRecognizerModule recModule;

	if( !recModule.LoadSVM(svmPath,svmFile) )
	{
		cout << "Could not load the support vector machine." << endl;
	}
	recModule.SetFeatureExtractor(&morphFeatureExtractor); 
	// capture is read only I think.  
	IplImage * img = NULL;
	int threshold = 150; 
 	bool quit = false;
#ifdef USE_DSHOW
	VI.getPixels(camNum, buffer, false, true);	
	img = cvCreateImage(cvSize(VI.getWidth(camNum),VI.getHeight(camNum)),IPL_DEPTH_8U,3);
	memcpy(img->imageData, buffer,VI.getSize(camNum)); 
#else
	while( img == NULL )
	{
		img = cvQueryFrame(capture); 
	}
#endif

	bool writeToFile = false;
	CvVideoWriter* writer = NULL;
	if( writeToFile )
	{
		writer = cvCreateVideoWriter("testvideo.avi",0,20, cvSize(img->width,img->height));
	}

	IplImage * img2 = cvCreateImage(cvSize(img->width,img->height), img->depth,img->nChannels);
	string windowName = "classifier";
	cvNamedWindow(windowName.c_str() ); 
	cvShowImage(windowName.c_str(),img);
	int key = 0;
	int count = 0;
	while( !quit )
	{
#ifdef USE_DSHOW
	VI.getPixels(camNum, buffer, false, true);	
	memcpy(img->imageData,buffer,VI.getSize(camNum));
#else	
	img = cvQueryFrame(capture); 
#endif
		cvCopy(img,img2);
		SRecObjVec objects = recModule.RecognizeFrame(img2);
		recModule.WriteObjectsToFrame(img2,objects);

		cvShowImage(windowName.c_str(),img2);
		key = cvWaitKey(1);
		if( key == 'q' || key == 'Q' )
		{
			quit = true; 
		}
		if( key == ' ' )
		{
			recModule.ExtractDetectedObjects(img2,savePath,fname);
		}
		if( key == 'a' )
		{
			
			threshold = (threshold + 5)%255;
			recModule.SetThreshold(threshold);
			cout << "Threshold: " << threshold << endl;
		}
		if( key == 's' )
		{
			threshold = (threshold - 5)%255;
			recModule.SetThreshold(threshold);
			cout << "Threshold: " << threshold << endl;
		}
		if( writeToFile )
		{
			cvWriteFrame(writer,img2); 
		}
	}
	cvDestroyWindow(windowName.c_str());  
	cvReleaseVideoWriter(&writer);
	cvReleaseImage(&img2);
#ifdef USE_DSHOW
	VI.stopDevice(camNum);
	if( NULL != buffer )
	{
		delete [] buffer;
	}
#else	
	cvReleaseCapture(&capture);  
#endif
	
	return;
}

